/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import transervfx.Dato.Entidad.TipoEncomienda;
import transervfx.Dato.Entidad.TipoServicio;
import transervfx.Dato.Entidad.TipoTramite;

/**
 *
 * @author meli
 */
public class TipoServicioDAO implements Dao<TipoServicio,Long>{
    
    private static TipoServicioDAO INSTANCE = null;
    
    protected TipoServicioDAO() {
    }
    
    
        private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TipoServicioDAO();
        }
    }

    /**
     * Obtiene la instancia.
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static TipoServicioDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }
    

    @Override
    public void persist(TipoServicio entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
    public void remove(TipoServicio entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.remove(entity);

        } finally {
            em.close();
        }
    }

    @Override
    public void update(TipoServicio bean) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

          em.getTransaction().begin();
          if(em.contains(bean))
          {
            em.merge(bean);
          }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
    }

    @Override
    public TipoServicio findById(Long id) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(TipoServicio.class, id);

        } finally {
            em.close();
        }
    }

    @Override
    public List<TipoServicio> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("TipoServicio.findAll").getResultList();

        } finally {
            em.close();
        }
    }
    
    public List<TipoEncomienda> findAllTipoEncomienda() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("TipoEncomienda.findAll").getResultList();

        } finally {
            em.close();
        }
    }

    public List<TipoTramite> findAllTipoTramite() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("TipoTramite.findAll").getResultList();

        } finally {
            em.close();
        }
    }
    
}
